package com.forestar.mapControl.utils.mapConvert.task;

import android.graphics.Bitmap;
import android.graphics.Rect;

import com.forestar.mapControl.mapview.CacheCanvas;
import com.forestar.mapControl.mapview.multitouch.TestTextureViewRefreshListener;
import com.forestar.mapControl.utils.LogD;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * created by  xzk
 * 2020/8/5   12:14
 * 说明:  之前公共的绘制方法类，包含了好几个测试代码
 */
public class DrawForCommonTask extends DrawForTask {

    public DrawForCommonTask(TestTextureViewRefreshListener refreshListener,
                             AtomicInteger hasTaskNum, List<CacheCanvas> cacheCanvasList) {
        super(refreshListener, hasTaskNum);
        this.cacheCanvasList = cacheCanvasList;
    }

    @Override
    public int drawBitmapTask() {
        millSum=new ArrayList<>();
//            getAllTileFromFile();
//            tempPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
//            targetCanvas.getCanvas().drawPaint(tempPaint);
//            tempPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));

//            LogD.d("开始渲染："+bitmapTempList.size() );
//            Rect temp = new Rect(0,0,128,128);
        int startX=-(widthScreen/2);
        int startY=-(heightScreen/2);
        //计算一个屏幕需要多少张瓦片  上下左右
        offsetNum = widthScreen*2/tileSizeForShow;
        offsetNumY = heightScreen*2/tileSizeForShow;
        LogD.d("一共需要多少张瓦片:"+offsetNum+"x"+offsetNumY+"="+(offsetNum*offsetNumY));

        Rect rect = new Rect(startX,startY,startX+tileSizeForShow,startY+tileSizeForShow);
        int i= 1-offsetNum;
//            Rect sourceRect = new Rect(0,0,tileSizeForShow*5,tileSizeForShow*5);

        if(null == cacheCanvasList || cacheCanvasList.isEmpty()){
            return 0;
        }

        int allNum = 0;
        int nowNum = 0;
        int targetCanvasIndex = 0;
        //总共10层
        long startForCanvasMill =0;
        long endForCanvasMill =0;
        for (int j = 0; j < bitmapTempList.size(); j++) {
            Bitmap bitmap = bitmapTempList.get(j);
            if (null == bitmap || bitmap.isRecycled()) {
                continue;
            }
            if(targetCanvasIndex>cacheCanvasList.size()-1){
                break;
            }
            if(startForCanvasMill<1){
                startForCanvasMill = System.currentTimeMillis();
            }
            CacheCanvas cacheCanvas = cacheCanvasList.get(targetCanvasIndex);
            cacheCanvas.getCanvas().drawBitmap(bitmap, null, rect, tempPaint);
            cacheCanvas.getCanvas().drawRect(rect.left, rect.top, rect.right, rect.bottom, boxPaint);
            allNum++;

            nowNum++;
            if(nowNum!= 0 && nowNum%1000==0){
                if(endForCanvasMill<1){
                    endForCanvasMill = System.currentTimeMillis();
                }
                LogD.d("瓦片文件数量：本次绘制单层的时间："+(endForCanvasMill-startForCanvasMill));
                millSum.add("本次绘制单层的时间："+(endForCanvasMill-startForCanvasMill));
                startForCanvasMill =0;
                endForCanvasMill =0;
                targetCanvasIndex++;
                //初始化
                i = 1-offsetNum;
                rect = new Rect(startX,startY,startX+tileSizeForShow,startY+tileSizeForShow);
            }else{
                //在当前图层绘制
                if (i % offsetNum != 0) {
                    rect.left = rect.left + tileSizeForShow;
                    rect.right = rect.right + tileSizeForShow;
                    rect.top = rect.top;
                    rect.bottom = rect.bottom;
                } else {
                    rect.left = startX;
                    rect.right = startX+tileSizeForShow;
                    rect.top = rect.top + tileSizeForShow;
                    rect.bottom = rect.bottom + tileSizeForShow;
                }
                i++;
            }

        }
        for (int j = 0; j < cacheCanvasList.size(); j++) {
            startForCanvasMill = System.currentTimeMillis();
            targetCanvas.draw(cacheCanvasList.get(j));
            endForCanvasMill = System.currentTimeMillis();
            LogD.d("瓦片文件数量：本次绘制单层到缓存的时间："+(endForCanvasMill-startForCanvasMill));
            millSum.add("本次绘制单层到缓存的时间："+(endForCanvasMill-startForCanvasMill));
        }
//            //总共10层
//            for (int j = 0; j <cacheCanvasList.size(); j++) {
//                CacheCanvas cacheCanvas = cacheCanvasList.get(j);
//                List<Bitmap> bitmaps = bitmapTempList.get(j);
//                for (Bitmap uiBitmap : bitmaps) {
//                    if (null == uiBitmap || uiBitmap.isRecycled()) {
//                        continue;
//                    }
//                    //带缩放效果  todo 这个函数是什么意思？
//                    cacheCanvas.getCanvas().drawBitmap(uiBitmap, null, rect, tempPaint);
//                    cacheCanvas.getCanvas().drawRect(rect.left, rect.top, rect.right, rect.bottom, boxPaint);
//                    allNum++;
//
//                    if (i % offsetNum != 0) {
//                        rect.left = rect.left + tileSizeForShow;
//                        rect.right = rect.right + tileSizeForShow;
//                        rect.top = rect.top;
//                        rect.bottom = rect.bottom;
//                    } else {
//                        rect.left = startX;
//                        rect.right = startX+tileSizeForShow;
//                        rect.top = rect.top + tileSizeForShow;
//                        rect.bottom = rect.bottom + tileSizeForShow;
//                    }
//                    i++;
//                }
//                long end = System.currentTimeMillis();
//                LogD.d("瓦片文件数量:单层渲染："+allNum+"<-- 渲染用时-->"+(end-start));
//                //初始化一次
//                rect = new Rect(0,0,tileSizeForShow,tileSizeForShow);
//                i= 1-offsetNum;
//            }
//            for(List<Bitmap> uiBitmaps:bitmapTempList) {
//                if (null == uiBitmaps || uiBitmaps.isEmpty()) {
//                    continue;
//                } else {
//                    for (Bitmap uiBitmap : uiBitmaps) {
//                        if (null == uiBitmap || uiBitmap.isRecycled()) {
//                            continue;
//                        }
//                        allNum++;
//                        //带缩放效果  todo 这个函数是什么意思？
//                        targetCanvas.getCanvas().drawBitmap(uiBitmap, null, rect, tempPaint);
//                        targetCanvas.getCanvas().drawRect(rect.left, rect.top, rect.right, rect.bottom, boxPaint);
//
//                        if (i % offsetNum != 0) {
//                            rect.left = rect.left + tileSizeForShow;
//                            rect.right = rect.right + tileSizeForShow;
//                            rect.top = rect.top;
//                            rect.bottom = rect.bottom;
//                        } else {
//                            rect.left = 0;
//                            rect.right = tileSizeForShow;
//                            rect.top = rect.top + tileSizeForShow;
//                            rect.bottom = rect.bottom + tileSizeForShow;
//                        }
//                        i++;
//                    }
//                    //初始化一次
//                    rect = new Rect(0,0,tileSizeForShow,tileSizeForShow);
//                    i= 1-offsetNum;
//                }
//            }
//            rect = new Rect(offsetSizeTwice,offsetSizeTwice,offsetSizeTwice+tileSizeForShow,tileSizeForShow+offsetSizeTwice);
//            i=-6;
//            for(Bitmap uiBitmap:bitmapTempList){
//                if(null == uiBitmap || uiBitmap.isRecycled()){
//                    continue;
//                }
//                targetCanvas.getCanvas().drawBitmap(uiBitmap,null,rect,tempPaint);
//                targetCanvas.getCanvas().drawRect(rect.left ,rect.top ,rect.right ,rect.bottom,boxPaint);
//
//                if(i%7!=0){
//                    rect.left=rect.left+tileSizeForShow;
//                    rect.right=rect.right+tileSizeForShow;
//                    rect.top=rect.top ;
//                    rect.bottom=rect.bottom;
//                }else {
//                    rect.left=offsetSizeTwice;
//                    rect.right=offsetSizeTwice+tileSizeForShow;
//                    rect.top=rect.top+tileSizeForShow;
//                    rect.bottom=rect.bottom+tileSizeForShow;
//                }
//                i++;
//            }
        //分割红线，先隐藏
//            targetCanvas.getCanvas().drawLine(200,0
//                    ,200,2600,textPaint);
//            for (int j = 0; j < cacheCanvasList.size(); j++) {
//                cacheCanvasList.get(j).getCanvas().drawText("瓦片文件数量："+allNum+"<--渲染用时->"+(end-start)
//                        ,150,100,textPaint);
//            }
//            for (int k = 0; k < millSum.size(); k++) {
//                String sContent = millSum.get(k);
//                LogD.d("瓦片文件数量：总计："+sContent);
//                targetCanvas.getCanvas().drawText(sContent
//                        ,60,50+k*80,textPaint);
//            }

        return allNum;
    }

}
